package com.gy.sb.sbmqrabbit;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

import com.gy.sb.sbmqrabbit.compnent.Sender;

@SpringBootApplication
public class SbMqRabbitApplication {

	public static void main(String[] args) {

		ConfigurableApplicationContext context = SpringApplication.run(SbMqRabbitApplication.class, args);
		Sender sender = context.getBean("sender", Sender.class);
		sender.sendMsg("测试Spring AMQP发送消息");
	    try {
	        Thread.sleep(1000);
	    } catch (InterruptedException e) {
	        e.printStackTrace();
	    }
	    context.close();

	}

	@Bean
	CachingConnectionFactory myConnectionFactory() {
	    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
	    connectionFactory.setUsername("admin");
	    connectionFactory.setPassword("pwd123");
	    connectionFactory.setHost("192.168.1.101");
	    connectionFactory.setPort(5672);
	    connectionFactory.setVirtualHost("/");
	    return connectionFactory;
	}

	@Bean
	Exchange myExchange() {
	    return ExchangeBuilder.topicExchange("test.topic").durable(true).build();
	}

	@Bean
	Queue myQueue() {
	    return QueueBuilder.durable("myQueue").build();
	}

	@Bean
	public Binding myExchangeBinding(@Qualifier("myExchange") Exchange topicExchange,
	                                 @Qualifier("myQueue") Queue queue) {
	    return BindingBuilder.bind(queue).to(topicExchange).with("test.#").noargs();
	}

	@Bean
	public RabbitTemplate myExchangeTemplate(CachingConnectionFactory myConnectionFactory) {
	    RabbitTemplate rabbitTemplate = new RabbitTemplate(myConnectionFactory);
	    rabbitTemplate.setExchange("test.topic");
	    rabbitTemplate.setRoutingKey("test.abc.123");
	    return rabbitTemplate;
	}

}
